import {
  defineStore
} from 'pinia'
export const useUserStore = defineStore('user', {
  state: () => {
    return {
      user: {
        username: "立即登录",
        avatarUrl: "https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132",
        deftavatarUrl: "https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132",
        token: "",
        refresh_token: "",
        openid: "",
        status: "idle",
        iscollect: "false" //用户是否收藏该商品
      },
      //用户砍价列表
      userbargainlist: {
        list: []
      },
      //用户收藏列表
      usercollect: {
        list: []
      },
      //用户拼团列表
      pintuanlist: {
        list: []
      }
    }
  },
  actions: {
    //更改用户名
    setnickName(username) {
      this.user.username = username
    },
    //更改头像
    setavatarUrl(avatarUrl) {
      this.user.avatarUrl = avatarUrl
    },
    //获取openid
    getopenid() {
      //获取openid
      uni.login({
        provider: 'weixin',
        success: (res) => {
          // res.code 即code为获取到的 code
          wx.request({
            url: `https://api.weixin.qq.com/sns/jscode2session?appid=wxe4b3c7547529c2f3&secret=abc28946f28f0752f61c76e9a4838cc0&js_code=${res.code}&grant_type=authorization_code`,
            success: (res) => {

              this.user.openid = res.data.openid
            }
          })
        }
      });
    },
    //登录函数
    async Login() {
      const data = {
        username: this.user.username,
        avatarUrl: this.user.avatarUrl,
        openid: this.user.openid
      }
      //发送请求
      const response = await uni.$http.post("/user/login", data)
      if (response.statusCode === 200) {
        uni.showToast({
          title: '登录成功',
          duration: 2000,
          icon: "success"
        });
        this.user.token = response.data.access_token
        this.user.refresh_token = response.data.refresh_token
      } else {
        uni.showToast({
          title: '登录失败',
          duration: 2000,
          icon: "error"
        });
      }

    },

    //收藏
    async collect(goods_id) {
      const response = await uni.$http.put("/user/collect", {
        "goods_id": goods_id
      })
      if (response.statusCode === 200) {
        this.Iscollect(goods_id)
        uni.showToast({
          title: response.data,
          duration: 2000,
          icon: "success"
        });

      }
    },
    //判断是否收藏
    async Iscollect(goods_id) {
      const response = await uni.$http.put("/user/Iscollect", {
        "goods_id": goods_id
      })
      if (response.statusCode === 200) {
        this.user.iscollect = response.data
      }
    },
    //获取用户收藏列表
    async getcollectlist() {
      const response = await uni.$http.get("/user/getcollectlist")
      if (response.statusCode === 200) {
        this.usercollect.list = response.data
      }
    },
    //获取用户砍价列表
    async getbargainlist() {
      const response = await uni.$http.get("/user/getbargainlist")
      if (response.statusCode === 200) {
        this.userbargainlist.list = response.data
      }
    },
    //取消砍价
    async cancelbargain(goods_id) {
      const response = await uni.$http.delete("/user/cancelbargain", {
        "goods_id": goods_id
      })
      if (response.statusCode === 200) {
        this.getbargainlist()
        uni.showToast({
          title: response.data,
          duration: 2000,
          icon: "success"
        });
      }
    },
    //获取用户拼团列表
    async getpintuanlist() {
      const response = await uni.$http.get("/user/getpintuanlist")
      if (response.statusCode === 200) {
        this.pintuanlist.list = response.data
      }
    },
    //取消拼团
    async cancelpintuan(goods_id) {
      const response = await uni.$http.delete("/user/cancelpintuan", {
        "goods_id": goods_id
      })
      if (response.statusCode === 200) {
        this.getpintuanlist()
        uni.showToast({
          title: response.data,
          duration: 2000,
          icon: "success"
        });
      }
    },
    //换token
    async updatetoken() {
      const response = await uni.$http.post("/user/updatetoken", {
        refresh_token: this.user.refresh_token
      })
      this.user.token = response.data.token
      this.user.refresh_token = response.data.refresh_token
    }
  },
  unistorage: true // 开启后对 state 的数据读写都将持久化
})